# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()

# Definition for singly-linked list


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


import random


class Solution:
    def __init__(self, head: ListNode):
        self.m_list = []
        while head:
            self.m_list.append(head.val)
            head = head.next
        self.max_index = len(self.m_list) - 1

    def getRandom(self) -> int:
        return self.m_list[random.randint(0, self.max_index)]

